import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:study_flutter/yzhg/http/dio/DioUtils.dart';

class NetWorkPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new NetWorkPageItem();
  }
}

class NetWorkPageItem extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _NetWorkPageItem();
  }
}

class _NetWorkPageItem extends State<NetWorkPageItem> {
  //定义一个字符串
  String netString = "";

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("dio网络请求"),
      ),
      body: Container(
        alignment: Alignment.topCenter,
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                RaisedButton(
                  onPressed: () {
                    getRequest();
                  },
                  child: Text("Get请求"),
                ),
                RaisedButton(
                    onPressed: () {
                      postRequest();
                    },
                    child: Text("Post请求")),
              ],
            ),
            Text(netString,
                style: TextStyle(fontSize: 12, color: Colors.indigo))
          ],
        ),
      ),
    );
  }

  void getRequest() async {
    String result = await DioUtils.getInstance().get("banner/json");
    print("拿到的结果======================" + result.toString());
    this.setState(() {
      netString = result.toString();
      Fluttertoast.showToast(
          msg: netString,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
         /* timeInSecForIos: 1,*/
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);
    });
  }

  /**
   * post请求
   */
  void postRequest() async {
    var params = {
      "username": "dyc123456",
      "password": 123456,
      "repassword": 123456
    };
    String result =
        await DioUtils.getInstance().post("user/register", data: params);
    this.setState(() {
      netString = result;
      Fluttertoast.showToast(
          msg: netString,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);
    });
  }
}
